import os
from shutil import copyfile

input_1 = '/home/yjy/dataset/CASIA-PalmprintV1'
out_train = './images/train'
out_test = './images/test'


def is_left(file_name):
    return file_name.find('_l_') > 0


def get_pid(file_name):
    return int(file_name[:4])


def get_iid(file_name):
    return int(file_name[-2:0])


def cp(path_a, file_a, path_b, file_b=None):
    if file_b is None:
        file_b = file_a
    copyfile(os.path.join(path_a, file_a), os.path.join(path_b, file_b))


if __name__ == '__main__':
    # num_iid = 8
    # th = num_iid / 2
    dirs = os.listdir(input_1)
    for dir in dirs:
        if not os.path.isdir(os.path.join(input_1, dir)):
            continue
        files = os.listdir(os.path.join(input_1, dir))
        for ifile in files:
            if ifile == 'Thumbs.db':
                continue
            if is_left(ifile):
                cp(os.path.join(input_1, dir), ifile, out_train)
            else:
                cp(os.path.join(input_1, dir), ifile, out_test)
